package com.sanpu.kxe.common.view;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.style.ReplacementSpan;

import com.sanpu.kxe.utils.ScreenUtil;

/**
 * Description：圆角背景色Span
 * Created by 薛勇军 on 2018/9/5.
 */

public class RoundBackgroundColorSpan extends ReplacementSpan {

    private int bgColor;
    private int textColor;

    public RoundBackgroundColorSpan(int bgColor, int textColor) {
        super();
        this.bgColor = bgColor;
        this.textColor = textColor;
    }
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        //设置宽度为文字宽度加14dp
        return ((int)paint.measureText(text, start, end) + ScreenUtil.dp2px(14));
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        int color1 = paint.getColor();
        paint.setColor(this.bgColor);
        //画圆角矩形背景
        canvas.drawRoundRect(new RectF(x,
                top+ ScreenUtil.dp2px(2),
                x + ((int) paint.measureText(text, start, end)+ ScreenUtil.dp2px(14)),
                bottom- ScreenUtil.dp2px(2)),
                ScreenUtil.dp2px(3), ScreenUtil.dp2px(3), paint);
        paint.setColor(this.textColor);
        //画文字,两边各增加7dp
        canvas.drawText(text, start, end, x+ ScreenUtil.dp2px(7), y- ScreenUtil.dp2px(0.5f), paint);
        paint.setColor(color1);
    }
}
